package com.ccx.coachsite.ui.own;

import com.ccx.coachsite.R;
import com.ccx.coachsite.bus.CommonBus;
import com.ccx.coachsite.bus.SharedKey;
import com.ccx.coachsite.http.BaseResp;
import com.ccx.coachsite.util.CommonViewUtils;
import com.ccx.coachsite.util.SharedPreferencesUtil;
import com.ccx.coachsite.util.StringUtil;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.Toast;

public class OwerMoreSuggestView extends OwerBaseView{

	private View mSave; // 保存
	private EditText mSuggestText; // 旧密码
	private EditText mPhoneText; // 新密码
	private CommonBus bo;
	
	public OwerMoreSuggestView(Context context, OwerMainBase superView) {
		super(context, superView);
		
		mSuggestText = (EditText) findViewById(R.id.ower_more_feedback_content);
		mPhoneText = (EditText) findViewById(R.id.ower_more_feedback_connect);
		String mobile = SharedPreferencesUtil.getValue(context, SharedKey.Shared_Mobile_Key, "");
		mPhoneText.setText(mobile);
		
		mSave = findViewById(R.id.ower_more_feedback_submit);
		mSave.setOnClickListener(new OnSave());
	}
	
	class OnSave implements OnClickListener {
		@Override
		public void onClick(View v) {
			sendRequest();
		}
	}
	
	private boolean checkInput() {
		Animation shake = AnimationUtils.loadAnimation(mContext, R.anim.shake);

		String suggest = mSuggestText.getText().toString().trim();
		String phone = mSuggestText.getText().toString().trim();
		
		if (StringUtil.isEmpty(suggest)) {
			CommonViewUtils.showErrorByEditText(mSuggestText, "请输入意见内容", shake);
			return false;
		}

		if (StringUtil.isEmpty(phone)) {
			CommonViewUtils.showErrorByEditText(mPhoneText, "请输入联系方式", shake);
			return false;
		}
		
		return true;
	}
	
	private void sendRequest() {
		if (!checkInput())
			return;

		CommonViewUtils.hideKeyboard((Activity)mContext);
		
		if (!CommonViewUtils.isNetConnectionAvailable(mContext)) {
			Toast.makeText(mContext, R.string.login_network_error, Toast.LENGTH_SHORT).show();
			return;
		}
		
		if (bo == null) {
			bo = new suggestBus(mContext);
		}

		final String suggest = mSuggestText.getText().toString();
		final String mobile = mPhoneText.getText().toString();
		bo.setSuggestParam(suggest, mobile);
		bo.doTask();
	}
	
	class suggestBus extends CommonBus{

		public suggestBus(Context context) {
			super(context);
		}
		
		@Override
		public void callBack(int taskType, BaseResp response, String responseJson) {
			super.callBack(taskType, response, responseJson);
			
			if(response.isSuccess())
			{
				mSuperView.onBackClick();
			}
			else
			{

			}
		}
	}
}
